import {actionType} from './index';
import { fromJS } from 'immutable';
import axios from 'axios';

const changeStateHomeItemList = (value)=>{
   return {
       type:actionType.CHNAGE_ITEMLIST,
       ItemList:fromJS(value.itemList),
       users:value.users,
       allUsersPage:Math.ceil(value.users.length/5),
   }
}
const changeStateHomedataNote = (value)=>{
    return {
        type:actionType.CHNAGE_NOTELIST,
        dataNote:fromJS(value)
    }
 }
 const addStateHomedataNote = (value,nextPage)=>{
    return {
        type:actionType.ADD_NOTELIST,
        data:fromJS(value),
        nextPage
    }
 }
export const getHomeItemList = ()=>{
    return (dispath) =>{
        axios.get('/home.json')
        .then( (response)=> {
            if(response.data.code === '200'){
                dispath(changeStateHomeItemList(response.data));
            }
        })
        .catch( (error)=> {
            console.log(error);
        });
    }
}
export const getHomeNoteList = ()=>{
      return (dispath)=>{
        axios.get('/home.json')
        .then( (response)=> {
            if(response.data.code === '200'){
                dispath(changeStateHomedataNote(response.data.dataNote));
            }
        })
        .catch( (error)=> {
            console.log(error);
        });
      }
}
export const getMoreNoteList = (page)=>{
    return (dispath)=>{
        axios.get('/homeList.json?page='+page)
        .then( (response)=> {
            if(response.data.success){
                dispath(addStateHomedataNote(response.data.data,page+1));
            }
        })
        .catch( (error)=> {
            console.log(error);
        });
    }
}
export const changeUserPage = (page)=>{
      return {
          type:actionType.CAHNGE_USERPAGE,
          page
      }
}
export const handleFollowUser = (id)=>{
    return {
        type:actionType.CHANGE_FOLLOWUSER,
        id
    }
}
export const showTopTip = (isShow)=>{
    return {
        type:actionType.CHANGE_SHOWTIP,
        isShow
    }
}